#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'tian'
__data__ = '2022/3/15 10:28'
# software: PyCharm

# 独立使用django的model

"""
添加分类
"""

import sys
import os

pwd = os.path.dirname(os.path.abspath(__file__))
sys.path.append(pwd)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "newsshopping.settings")

import django

django.setup()

from apps.goods.models import GoodsCategory
from scripts.data.category_data import row_data

for lev1 in row_data:
    # print(lev1.get("code"))
    #     # print(lev1.get("name"))
    good_obj = GoodsCategory()
    good_obj.code = lev1.get("code")
    good_obj.name = lev1.get("name")
    good_obj.category_type = 1
    good_obj.save()

    for lev2_cat in lev1.get("sub_categorys"):
        lev2_obj = GoodsCategory()
        lev2_obj.code = lev2_cat.get("code")
        lev2_obj.name = lev2_cat.get("name")
        lev2_obj.category_type = 2
        lev2_obj.parent_category = good_obj
        lev2_obj.save()

        for lev3_cat in lev2_cat["sub_categorys"]:
            lev3_obj = GoodsCategory()
            lev3_obj.code = lev3_cat["code"]
            lev3_obj.name = lev3_cat["name"]
            lev3_obj.category_type = 3
            lev3_obj.parent_category = lev2_obj
            lev3_obj.save()
